﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.DAL;
using PBOffice.BLL;

public partial class MsgMng_newsPublish : System.Web.UI.Page
{
    News news = new News();
    NewsManager nm = new NewsManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        //页面首次加载
        if (!IsPostBack)
        {
            List<NewsType> newsTypeList = nm.ShowNewsType();
            ddlNewsType.DataSource = newsTypeList;            //绑定新闻类型
            ddlNewsType.DataTextField = "NewsTypeId";
            ddlNewsType.DataValueField = "NewsTypeNo";
            ddlNewsType.DataBind();
            ddlNewsType.SelectedIndex = 0;        //默认选中第一项         
        }
        else
        {
            txtNewsTitle.Text = "";     //清空标题
            edtNews.Content = "";        //清空内容
            rbtnlIsTop.SelectedIndex = 0;  //默认置顶
            rbtnIsRecommend.SelectedIndex = 0;  //默认推荐
            ddlNewsType.SelectedIndex = 0;        //默认新闻类型选中第一项
        }

    }
    //提交按钮单击事件
    protected void btnOK_Click(object sender, EventArgs e)
    {
        news.NewsTitle = txtNewsTitle.Text.Trim();     //新闻标题
        int newsTypeNo=Convert.ToInt32(ddlNewsType.SelectedValue.ToString());    //新闻类型
        User user = (User)Session["userManage"];
        int userNo = user.UserNo;
        if (rbtnlIsTop.SelectedIndex == 0)                  
            news.IsTop = true;            //选中第一项：置顶
        else
            news.IsTop = false;           //选中第二项：不置顶
        if (rbtnIsRecommend.SelectedIndex == 0)
            news.IsRecommend = true;        //选中第一项：推荐
        else
            news.IsRecommend = false;       //选中第二项：不推荐
        news.NewsContent = edtNews.Content;  //新闻内容
        news.PublishDate = DateTime.Now;     //发表时间
        news.ClickNum = 0;                   //点击量
        bool result = nm.AddNewsInfo(news,userNo,newsTypeNo);    //添加新闻到数据库
        if (result == true)        //提示信息
        {
            Response.Write("<script>alert('添加成功！')</script>");
            Page_Load(sender,e);
        }
        else
        {
            Response.Write("<script>alert('添加失败！')</script>");
        }
    }
}